package com.wanxi.dao.Impl;

import com.wanxi.dao.MajorDao;
import com.wanxi.model.MajorModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Li
 * @Date 2023/5/15 16:26
 */
public class MajorDaoImpl implements MajorDao {
    @Override
    public boolean findSameName(MajorModel majorModel) {
        String sql = "select * from `major` where name='" + majorModel.getName() + "' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public int add(MajorModel majorModel) {
        String sql = " INSERT INTO `major` (`name`, `image`,  `create_time`, `update_time`, `enable`, `remark`,`major_id`)" +
                " VALUES ('" + majorModel.getName() + "', '" + majorModel.getImage() + "'," +
                "now(), sysdate(), '" + majorModel.getEnable() + "', '" + majorModel.getRemark() + "','" + majorModel.getMajorId() + "');";
        return JDBC.update(sql);
    }

    @Override
    public int update(MajorModel majorModel) {
        String sql = " UPDATE `major` SET `name` = '" + majorModel.getName() + "', `image` = '" + majorModel.getImage() + "', `major_id` = '" + majorModel.getMajorId() + "', " +
                " `update_time` = now(), `enable` = '" + majorModel.getEnable() + "'," +
                " `remark` = '" + majorModel.getRemark() + "' WHERE (`id` = '" + majorModel.getId() + "');";
        return JDBC.update(sql);
    }

    @Override
    public int delete(MajorModel majorModel) {
        String sql = " delete from `major` where id = '" + majorModel.getId() + "' ";
        return JDBC.update(sql);
    }

    @Override
    public List<MajorModel> findAll(MajorModel majorModel) {
        int start = (majorModel.getPage()-1)*majorModel.getLimit();
        String sql = " select * from (select a.*,b.service_type serviceName from major as a left join service_area as b on a.major_id=b.id) temp where true";
        sql += getWhereSql(majorModel);
        sql += " order by id desc";
        sql += " limit "+start+","+majorModel.getLimit()+" ";
        ResultSet query = JDBC.query(sql);
        List<MajorModel> majorModelList = new ArrayList<>();
        try {
            while (query.next()) {
                majorModelList.add(MajorModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .image(query.getString("image"))
                        .isShow(query.getInt("is_show"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .majorId(query.getInt("major_id"))
                        .serviceName(query.getString("serviceName"))
                        .build());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return majorModelList;
    }

    @Override
    public MajorModel findById(MajorModel majorModel) {
        String sql = " select * from `major` where id = '" + majorModel.getId() + "' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return MajorModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .image(query.getString("image"))
//                        .isShow(query.getInt("is_show"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .majorId(query.getInt("major_id"))
                        .build();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private String getWhereSql(MajorModel majorModel) {
        String sql = "";
        if (!"".equals(majorModel.getName())) {
            sql += " and name like '%" + majorModel.getName() + "%' ";
        }
        if (majorModel.getEnable() != -1) {
            sql += " and enable = '" + majorModel.getEnable() + "' ";
        }
        return sql;
    }

    @Override
    public Integer count(MajorModel majorModel) {
        String sql = "select count(*) as count from `major` where true";
        sql += getWhereSql(majorModel);
        return JDBC.count(sql);

    }

    @Override
    public Integer totalCount() {
        String sql = "select count(*) as count from `major`";
        return JDBC.count(sql);
    }
}
